import random
import sys
import os


class Bill(object):
    """docstring for 创建发票"""

    def __str__(self):
        self.show()
        return self.info

    def __init__(self, price, f_date='2020-1-1', title='珠江水文水资源勘测中心', 税号='1210000074554351XY', 税率=6):
        super(Bill, self).__init__()

        # 大写金额转换
        self.num_dic = {'0': u'零', '1': u'壹', '2': u'贰', '3': u'叁',
                        '4': u'肆', '5': u'伍', '6': u'陆', '7': u'柒', '8': u'捌', '9': u'玖'}
        self.unit_dic = {0: u'仟', 1: '佰', 2: '拾',
                         3: '万', 4: '仟', 5: '佰', 6: '拾', 7: ''}
        self.decimal_dic = {0: u'角', 1: u'分', 2: u'厘', 3: u'毫'}

        # 其他详细信息
        self.校验码 = self.校验码()
        self.发票总价 = self.发票总价(price)
        self.税率 = str(float(税率) * 0.01)
        self.发票号码 = self.发票号码()
        self.发票金额 = self.发票金额(price, 税率)
        self.发票单价 = self.发票金额 + '0000'
        self.发票税额 = str(round(float(price) - float(self.发票金额), 2))
        self.总价大写 = self.convert_money(price)
        self.密码区 = self.密码区()
        self.发票日期 = self.发票日期(f_date)

        self.info = {
            '发票号码': self.发票号码,
            '发票日期': self.发票日期,
            '校验码': self.校验码,
            '密码区': self.密码区,
            '发票金额': self.发票金额,
            '发票金额2': '¥  ' + str(self.发票金额),
            '发票税额': self.发票税额,
            '发票税额2': '¥  ' + str(self.发票税额),
            '发票总价': self.发票总价,
            '总价大写': self.总价大写,
            '发票单价': self.发票单价,
            '发票抬头': title,
            '发票税号': 税号,
        }

    def 发票日期(self, d):
        return str(d)

    def 发票总价(self, tar):
        if not isinstance(tar, float):
            tar = float(tar)
        tar = "%.2f" % tar
        return '¥ ' + str(tar)

    def show(self):
        for (k, v) in self.info.items():
            print(str(k) + ':' + str(v))

    def 密码区(self):
        l1 = [i for i in range(0, 11)]
        l2 = [r'>', r'/', r'-', r'+', r'*', r'<']
        t = l1 + l2 + l1 + l2
        res = ''
        for i in range(120):
            num = random.randint(0, 33)
            res = res + str(t[num])
        # 每28位插入
        ret = list()
        ret.append(res[0:28])
        ret.append(res[28:56])
        ret.append(res[56:84])
        ret.append(res[84:112])
        return ret

    def 发票金额(self, 总价, 税率):
        return str(round(总价 / (1 + (税率 * 0.01)), 2))

    def 发票号码(self):
        return str(random.sample(range(10000000, 100000000), k=1)[0])

    def 校验码(self):
        res = ''
        tmp = random.sample(range(10000, 100000), k=4)
        for each in tmp:
            res = res + str(each) + ' '
        return res.strip(' ')

    def convert_int(self, num):
        temp = []
        t = str('%8d' % abs(num))  # 重点：把数字换成固定长度8位的字符串
        for s in range(0, 4):
            if t[s] != ' ' and t[s] != '0':
                temp.append(self.num_dic[t[s]])
                temp.append(self.unit_dic[s])
            if t[s] == '0' and s < 3 and t[s + 1] != '0':
                temp.append(self.num_dic[t[s]])
        if(t[2] != ' ' and t[3] == '0'):
            temp.append('万')
        for s in range(4, 8):
            if t[s] != ' ' and t[s] != '0':
                temp.append(self.num_dic[t[s]])
                temp.append(self.unit_dic[s])
            if t[s] == '0' and s < 7 and t[s + 1] != '0':
                temp.append(self.num_dic[t[s]])
        temp = ''.join(temp)
        if num < 0:
            temp = '负' + temp
        if num == 0:
            temp = '零' + temp

        return temp + u'圆'

    def convert_decimal(self, num):
        t = str('%4d' % num)
        temp = []
        for s in range(0, 4):
            if t[s] != ' ' and t[s] != '0':
                temp.append(self.num_dic[t[s]])
                temp.append(self.decimal_dic[s])
            if t[s] == '0' and s < 3 and t[s + 1] != '0':
                temp.append(self.num_dic[t[s]])
        temp = ''.join(temp)
        if len(temp) == 0:
            return '整'
        return temp

    def convert_money(self, money):
        num = round(float(money), 4)
        integer, decimal = str(num).split('.')
        result_int = self.convert_int(int(integer))
        result_decimal = self.convert_decimal(int(decimal))
        return result_int + result_decimal


if __name__ == '__main__':
    money = 22
    fp_date = '2020年4月15日'
    res = Bill(money, fp_date)
    res.show()
